package com.hww.acommon.test;

import android.content.Context;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.hww.common.MyAppThis;
import com.hww.common.util.PermissionUtils;
import com.hww.common.util.ToastUtils;
import com.hww.common.util.constant.Permission;

import java.util.Arrays;

/**
 * @author hww
 * @date 0017 2024/6/17
 * email: heww@zhongyuntech.cn
 * description:
 */
public class PermissionActivity extends BaseActivity {

    private static final int REQUEST_CODE = 0x1001;
    private Context context;

    /**
     * 步骤：
     * 1、初始化，设置activity对象： PermissionUtils.init(PermissionActivity.this);
     * 2、执行权限申请：PermissionUtils.requestPermissions
     * 3、设置回调接收内容：PermissionUtils.handleResult
     */

    @Override
    protected void initDate() {
        context = PermissionActivity.this;
        PermissionUtils.init(PermissionActivity.this);
        setGONE();

        setBTText(binding.bt1, "读写权限申请");
        setBTText(binding.bt2, "相机权限申请");
        setBTText(binding.bt3, "多个危险危险权限申请");
        setBTText(binding.bt4, "调整应用详情界面");

        binding.bt1.setOnClickListener((view) -> {
            System.out.println("读写权限申请");
            PermissionUtils.requestPermissions("", Permission.Group.STORAGE, callback);
        });

        binding.bt2.setOnClickListener((view) -> {
            PermissionUtils.requestPermission("", Permission.CAMERA, callback);
        });

        binding.bt3.setOnClickListener((view) -> {
            String permissions[] = new String[]{Permission.CALL_PHONE, Permission.READ_CALENDAR,
                    Permission.WRITE_CALENDAR, Permission.READ_CALL_LOG, Permission.WRITE_CALL_LOG};

            PermissionUtils.requestPermissions(null, permissions, callback);
        });

        binding.bt4.setOnClickListener((view) -> {
            PermissionUtils.toAppSetting(MyAppThis.getThis());
        });
    }

    PermissionUtils.PermissionCallback callback = new PermissionUtils.PermissionCallback() {
        @Override
        public void onGranted(String[] permissions) {
            ToastUtils.success(context, "已经通过申请的权限：\n" + Arrays.toString(permissions), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onDenied(String[] permissions) {
            ToastUtils.success(context, "用户拒绝：\n" + Arrays.toString(permissions), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onPermanentDenial(String[] permissions) {
            ToastUtils.success(context, "用户拒绝并勾选不再询问：\n" + Arrays.toString(permissions), Toast.LENGTH_LONG).show();
        }
    };

    /**
     * 权限请求处理的结果
     *
     * @param requestCode  请求码
     * @param permissions  字符串数组，包含了权限请求中所请求的所有权限名称。这个数组与发起权限请求时传入的权限数组相对应，用于匹配每个权限的授权状态
     * @param grantResults 整型数组，对应于permissions数组中的每一个权限，表示用户对该权限授权与否的状态
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionUtils.handleResult(requestCode, permissions, grantResults);
//        switch (requestCode) {
//            case REQUEST_CODE:
//                PermissionUtilsBak.onRequestMorePermissionsResult(context,
//                        permissions,
//                        new PermissionUtilsBak.PermissionCheckCallBack() {
//                            @Override
//                            public void onHasPermission() {
//                                StringBuilder builder = new StringBuilder();
//                                builder.append("权限申请成功").append("\n");
//                                for (String p : permissions) {
//                                    builder.append(p).append("\n");
//
//                                }
//                                Toast.makeText(MyAppThis.getThis(), builder.toString(), Toast.LENGTH_SHORT).show();
//                            }
//
//                            @Override
//                            public void onUserHasAlreadyTurnedDown(String... permission) {
//                                String toString = Arrays.toString(permission);
//                                System.out.println("用户拒绝的权限: " + toString);
//                                Toast.makeText(MyAppThis.getThis(), String.format("用户拒绝的权限: %s", toString), Toast.LENGTH_SHORT).show();
//                            }
//
//                            @Override
//                            public void onUserHasAlreadyTurnedDownAndDontAsk(String... permission) {
//                                String toString = Arrays.toString(permission);
//                                System.out.println("用户拒绝并勾选不再询问的权限: " + toString);
//                                Toast.makeText(MyAppThis.getThis(), String.format("用户拒绝并勾选不再询问的权限: %s", toString), Toast.LENGTH_SHORT).show();
//
//                            }
//                        });
//                break;
//            default:
//                break;
//        }
    }
}
